package com.ruoyi.project.product.lineManage.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.type.domain.CmsType;
import com.ruoyi.project.product.lineManage.domain.LineManage;
import com.ruoyi.project.product.lineManage.service.LineManageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/product/lineManage")
public class LineManageController extends BaseController {
    //声明业务层属性
    @Autowired
    private LineManageService lineManageServiceImpl;
    //声明资源跳转的公共前缀
    private String prefix = "product/lineManage";
    //声明单元方法:完成产品风格页面跳转
    @RequiresPermissions("product:lineManage:view")
    @GetMapping()
    public String productLineManage(){
        return prefix+"/LineManage";
    }

    /**
     * 声明单元方法，完成产品系列菜单的加载
     * @param
     * @return
     */
    @RequiresPermissions("product:lineManage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LineManage lineManage)
    {
        startPage();
        List<LineManage> list = lineManageServiceImpl.selectLineManageList(lineManage);
        return getDataTable(list);
    }

    /**
     * 新增单元方法:完成新增产品系列类型页面的转发
     * @return
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    /**
     * 校验类型名称
     */
    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkPostNameUnique(LineManage lineManage)
    {
        return lineManageServiceImpl.checkTypeNameUnique(lineManage);
    }

    /**
     * 新增保存产品系列
     */
    @RequiresPermissions("product:lineManage:add")
    @Log(title = "产品系列管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated LineManage lineManage)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(lineManageServiceImpl.checkTypeNameUnique(lineManage)))
        {
            return error("新增产品系列'" + lineManage.getTypeName() + "'失败，公告类型名称已存在");
        }
        return toAjax(lineManageServiceImpl.insertLineManage(lineManage));
    }

    /**
     * 修改产品系列，跳转修改页面并完成数据的回显
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
        mmap.put("LineManage", lineManageServiceImpl.selectTypeById(typeId));
        return prefix + "/edit";
    }

    /**
     * 修改保存产品系列
     */
    @RequiresPermissions("product:lineManage:edit")
    @Log(title = "产品系列管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated LineManage lineManage)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(lineManageServiceImpl.checkTypeNameUnique(lineManage)))
        {
            return error("修改产品系列类别'" + lineManage.getTypeName() + "'失败，产品系列名称已存在");
        }
        return toAjax(lineManageServiceImpl.updateLineManage(lineManage));
    }

    /**
     * 删除产品系列
     * @param ids
     * @return
     */
    @RequiresPermissions("product:lineManage:remove")
    @Log(title = "产品系列管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(lineManageServiceImpl.deleteLineManageByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

}
